package com.spider.gp.schedule;

import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * description:
 *
 * @author clare
 * @date 2022/09/22 09:33
 **/
//@Component
public class CommonSchedule implements SchedulingConfigurer {


    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            // 定时任务要执行的内容
            System.out.println("【开始执行定时任务。。。bbbbbbbbb】");
        }
    };

    Trigger trigger = new Trigger() {
        @Override
        public Date nextExecutionTime(TriggerContext triggerContext) {
             String cron = "0 0/1 * * * ?"; //可以将表达式配置在数据库中
            System.out.println("cron:" + cron);
            return new CronTrigger(cron).nextExecutionTime(triggerContext);
        }
    };

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        taskRegistrar.addTriggerTask(runnable, trigger);
    }

}








